//
//  NewsCollectionCell.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/20.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//


/*
 *
 *
 */
#import "CollectNewsCell.h"

#import "FashionNewsModel.h"
#import "NewsTableCell.h"
#import "FashionDetailView.h"

#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

#import "CollectNewsModel.h"
#import "CollectNewsDatabase.h"

@interface CollectNewsCell() <UITableViewDataSource,UITableViewDelegate,FashionDetailViewDelegate>
@property (nonatomic,retain) UITableView * table;
@property (nonatomic,retain) UIImageView * upImageView;
@property (nonatomic,retain) UIImageView * downImageView;
@property (nonatomic,retain) NSMutableArray * tableArray;
@property (nonatomic,retain) UILabel * zeroLabel;

@end

#define kCellHeight 215;

@implementation CollectNewsCell
-(void)dealloc{
    [_zeroLabel release];
    [_table release];
    [_upImageView release];
    [_downImageView release];
    [_tableArray release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableArray = [NSMutableArray array];
        
        [self createTable];

        [self loadModelFromDatabase];
        
    }
    return self;
}
// 从数据库读取
-(void)loadModelFromDatabase{
    NSArray * databaseArray = [CollectNewsDatabase selectAll];
    for (CollectNewsModel * collectModel in databaseArray) { // fileName为每个文件的文件名
        FashionNewsModel * model = [[FashionNewsModel alloc] init];
        model.contentid = collectModel.c_contentid;
        model.title = collectModel.c_title;
        model.thumb = collectModel.c_thumb;
        model.cat_logo = collectModel.c_cat_logo;
        [_tableArray addObject:model];
        [model release];
    }
    [_table reloadData];
    
    // 创建提示label，提示收藏是否为空
    self.zeroLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    _zeroLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
    _zeroLabel.text = @"没有收藏";
    _zeroLabel.font = [UIFont boldSystemFontOfSize:25];
    _zeroLabel.textColor = [UIColor redColor];
    _zeroLabel.textAlignment = NSTextAlignmentCenter;
    [_table addSubview:_zeroLabel];
    _zeroLabel.hidden = YES;
    [_zeroLabel release];
    
    // 如果收藏为空，提示label显示
    if(_tableArray.count == 0){
        _zeroLabel.hidden = NO;
    }
}

-(void)createTable{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.backgroundColor = [UIColor clearColor];
    
    _table.delegate = self;
    _table.dataSource = self;
    [self.contentView addSubview:_table];
    
    [_table release];
}

#pragma mark - UITableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * defaultCellIdentify = @"defaultCell";
    NewsTableCell * cell = [tableView dequeueReusableCellWithIdentifier:defaultCellIdentify];
    if(!cell){
        cell = [[NewsTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellIdentify];
    }
    [cell sendMessageWithModel:[_tableArray objectAtIndex:indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellHeight;
}
#pragma  mark - 当某一个cell被选中时，创建详情页
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsTableCell * cell = (NewsTableCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    // 根据contentOffSet和indexPath判断cell在屏幕上的的位置
    CGFloat cellHeightInScreen = tableView.contentOffset.y - indexPath.row*kCellHeight;
    
    float scaleIniPhone6 = 377.0/667.0; // 以iPhone6的比例为标准计算当前的高度
    float originalImageHeight =  [UIScreen mainScreen].bounds.size.height*scaleIniPhone6;
    
    FashionNewsModel * model = [_tableArray objectAtIndex:indexPath.row];
    
    FashionDetailView * fashionDetail = [[FashionDetailView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) image:cell.originalImage imageRect:CGRectMake(0, -cellHeightInScreen, self.frame.size.width, originalImageHeight) model:model];
    fashionDetail.delegate = self;
    [self.superview.superview addSubview:fashionDetail];
}
#pragma mark - FashionDetailViewDelegate 当详情页退出时刷新table
-(void)detailWillDisappear{
    [self viewWillAppearReloadData];
}
#pragma  mark - 当视图即将出现或者详情页收回时刷新table
-(void)viewWillAppearReloadData{
    [_tableArray removeAllObjects];
    NSArray * databaseArray = [CollectNewsDatabase selectAll];
    for (CollectNewsModel * collectModel in databaseArray) { // fileName为每个文件的文件名
        FashionNewsModel * model = [[FashionNewsModel alloc] init];
        model.contentid = collectModel.c_contentid;
        model.title = collectModel.c_title;
        model.thumb = collectModel.c_thumb;
        model.cat_logo = collectModel.c_cat_logo;
        [_tableArray addObject:model];
        [model release];
    }
    [_table reloadData];
    
    // 根据收藏内容设置提示label是否显示
    if(_tableArray.count == 0){
        _zeroLabel.hidden = NO;
    }else _zeroLabel.hidden = YES;
}
@end
